<template>
	<view class="e-line" :style="[lineStyle]">

	</view>
</template>

<script lang="ts">
	import {
		defineComponent
	} from 'vue'
	import mpMixin from "../../libs/mixin/mpMixin";
	/**
	 * line 线条
	 * @description 此组件一般用于显示一根线条，用于分隔内容块，有横向和竖向两种模式，且能设置0.5px线条，使用也很简单
	 * @tutorial https://www.uviewui.com/components/line.html
	 * @property {String}			color		线条的颜色 ( 默认 '#d6d7d9' )
	 * @property {String | Number}	length		长度，竖向时表现为高度，横向时表现为长度，可以为百分比，带px单位的值等 ( 默认 '100%' )
	 * @property {String}			direction	线条的方向，row-横向，col-竖向 (默认 'row' )
	 * @property {Boolean}			hairline	是否显示细线条 (默认 true )
	 * @property {String | Number}	margin		线条与上下左右元素的间距，字符串形式，如"30px"  (默认 0 )
	 * @property {Boolean}			dashed		是否虚线，true-虚线，false-实线 (默认 false )
	 * @property {Object}			customStyle	定义需要用到的外部样式
	 * @example <e-line color="red"></e-line>
	 */
	export default defineComponent({
		name: 'e-line',
		mixins: [mpMixin]
	})
</script>

<script lang="ts" setup>
	import {
		computed,
		toRefs
	} from 'vue'
	import {
		lineProps
	} from './props';
	import {
		deepMerge
	} from '../../libs/utils'
	import {
		useDefaultProps
	} from '../../libs/hooks'
	import {
		addUnit,
		addStyle
	} from '../../libs/utils/_utils'

	import componentsProps, {
		type DefaultProps
	} from "../../libs/config/componentsProps";

	const {
		line: lineDefaultProps
	} = toRefs(componentsProps);

	const _props = defineProps(lineProps)
	const props = useDefaultProps < DefaultProps['line'] > (_props, lineDefaultProps);
	
	const lineStyle = computed(() => {
		const style: Record < string, string | number > = {}
		style.margin = props.value.margin as string
		// 如果是水平线条，边框高度为1px，再通过transform缩小一半，就是0.5px了
		if (props.value.direction === 'row') {
			// 此处采用兼容分开写，兼容nvue的写法
			style.borderBottomWidth = '1px'
			style.borderBottomStyle = props.value.dashed ? 'dashed' : 'solid'
			style.width = addUnit(props.value.length  as string)
			if (props.value.hairline) style.transform = 'scaleY(0.5)'
		} else {
			// 如果是竖向线条，边框宽度为1px，再通过transform缩小一半，就是0.5px了
			style.borderLeftWidth = '1px'
			style.borderLeftStyle = props.value.dashed ? 'dashed' : 'solid'
			style.height = addUnit(props.value.length  as string)
			if (props.value.hairline) style.transform = 'scaleX(0.5)'
		}
		style.borderColor = props.value.color  as string
		return deepMerge(style, addStyle(props.value.customStyle as Record < string, any>) as Record < string, any>)
	})
</script>

<style lang="scss" scoped>
	@import "../../libs/theme/line.scss";
</style>
